package com.cskaoyan.service.promoteservice;

import com.cskaoyan.bean.promotebean.Ad;
import com.cskaoyan.bean.promotebean.AdExample;
import com.cskaoyan.bo.promoteBO.AdUpdateBO;
import com.cskaoyan.mapper.promotemapper.AdMapper;
import com.cskaoyan.vo.promotevo.AdCreateVO;
import com.cskaoyan.vo.promotevo.AdListVO;
import com.cskaoyan.vo.promotevo.AdUpdateVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AdServiceImpl implements  AdService{
    @Autowired
    AdMapper adMapper;
    @Override
    public AdListVO queryAdAll(Integer page, Integer limit,String name,String content, String sort, String order) {
        PageHelper.startPage(page, limit);
        AdExample adExample = new AdExample();
        //条件查询
        AdExample.Criteria criteria = adExample.createCriteria();
        if(name != null)criteria.andNameLike("%" + name + "%");
        if(content != null)criteria.andContentLike("%" + content + "%");
        criteria.andDeletedEqualTo(false);

        adExample.setOrderByClause(sort + " " + order);
        List<Ad> adList = adMapper.selectByExample(adExample);

        PageInfo<Ad> adPageInfo = new PageInfo<>(adList);
        long total = adPageInfo.getSize();

        AdListVO adVO = new AdListVO();
        adVO.setItems(adList);
        adVO.setTotal(total);
        return adVO;
    }

    @Override
    public AdCreateVO insert(Ad ad) {
        ad.setAddTime(new Date());
        ad.setUpdateTime(new Date());
        adMapper.insertSelective(ad);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(ad.getAddTime());
        String format1 = simpleDateFormat.format(ad.getUpdateTime());
        Byte positionAd = ad.getPosition();
        Integer position = 0;
        if(positionAd == 1){
            position = 1;
        }
        AdCreateVO adCreateVO = new AdCreateVO(ad.getId(),ad.getName(),ad.getLink(),ad.getUrl(),position,
        ad.getContent(),ad.getEnabled(),format,format1
        );
        return adCreateVO;
    }

    @Override
    public AdUpdateVO update(AdUpdateBO adBo) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date addTime = simpleDateFormat.parse(adBo.getAddTime());
//        Date updateTime = simpleDateFormat.parse(adBo.getUpdateTime());
        String addTime = simpleDateFormat.format(adBo.getAddTime());
        String updateTime = simpleDateFormat.format(adBo.getUpdateTime());
        Ad ad = new Ad(adBo.getId(),adBo.getName(),adBo.getLink(),adBo.getUrl(),adBo.getPosition(),
                adBo.getContent(),adBo.getEnabled(),adBo.getAddTime(),adBo.getUpdateTime(),adBo.getDeleted()
        );
        adMapper.updateByPrimaryKeySelective(ad);
        AdUpdateVO adUpdateVO = new AdUpdateVO(ad.getId(),ad.getName(),ad.getLink(),ad.getUrl(),
                ad.getPosition(),ad.getContent(),ad.getEnabled(), addTime,updateTime,ad.getDeleted()
        );
        return adUpdateVO;
    }

    @Override
    public void delete(Integer id) {
        adMapper.deleteByPrimaryKey(id);
    }

}
